<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8"/>
	<link rel="apple-touch-icon" sizes="76x76" href="./assets/img/favicon.ico">
	<link rel="icon" type="image/png" href="./assets/img/favicon.ico">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
	<title>数据要素流通平台</title>
	<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no' name='viewport'/>

	<!-- Google Font -->
	<!-- <link href="https://fonts.googleapis.com/css?family=Nunito:300,300i,400,600,800" rel="stylesheet"> -->

	<!-- Main CSS -->
	<link href="./assets/css/main.css" rel="stylesheet"/>

	<!-- Animation CSS -->
	<link href="vendor/aos.css" rel="stylesheet"/>

</head>

<body>



<!-- Main -->
<div class="d-md-flex h-md-100 align-items-center">
	<div class="col-md-6 p-0 bg-indigo h-md-100" style="background-image: url(./assets/img/demo/banner1.jpg);background-size: cover;">
		<div class="text-white d-md-flex align-items-center h-100 p-5 text-center justify-content-center">
			<div class="logoarea pt-5 pb-5">
				<p>
					<!-- <i class="fa fa-anchor fa-3x"></i> -->
				</p>
				<h1 class="mb-0 mt-3 display-4">数据要素流通平台</h1>
				<h5 class="mb-4 font-weight-light"> <!-- <i class="fab fa-sass fa-2x text-cyan"></i> --></h5>

			</div>
		</div>
	</div>
	<div class="col-md-6 p-0 bg-white h-md-100 loginarea">
		<div class="d-md-flex align-items-center h-md-100 p-5 justify-content-center">
			<form class="border rounded p-5" id="form1">
				<h3 class="mb-4 text-center">登录</h3>
				<div class="form-group">
					<input type="text" class="form-control" id="InputEmail1" placeholder="账号" required="" />
				</div>
				<div class="form-group">
					<input type="password" class="form-control" id="InputPassword1" placeholder="密码" required="" />
				</div>
				<div class="form-group form-check">
					<input type="checkbox" class="form-check-input" id="exampleCheck1">
					<label class="form-check-label small text-muted" for="exampleCheck1">记住我</label>
				</div>
				<button class="btn btn-success btn-round btn-block shadow-sm" type="submit">提交</button>
				<small class="d-block mt-4 text-center">
					<a class="text-gray" href="./forgetPassword.html" style="float: left;">忘记密码?</a>
					<a class="text-gray" href="./register.html" style="float: right;">前往注册</a>
				</small>
			</form>
		</div>
	</div>
</div>
<!-- End Main -->


<!--------------------------------------
JAVASCRIPTS
--------------------------------------->
<script src="vendor/jquery.min.js" type="text/javascript"></script>
<script src="vendor/popper.min.js" type="text/javascript"></script>
<script src="vendor/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/functions.js" type="text/javascript"></script>
<script src="./assets/layui/layui.js" type="text/javascript"></script>
<script src="js/tool.js" type="text/javascript"></script>
<!-- Animation -->
<script src="vendor/aos.js" type="text/javascript"></script>
<noscript>
	<style>
		*[data-aos] {
			display: block !important;
			opacity: 1 !important;
			visibility: visible !important;
		}
	</style>
</noscript>
<script>
	AOS.init({
		duration: 700
	});
</script>

<!-- Disable animation on less than 1200px, change value if you like -->
<script>
	AOS.init({
		disable: function () {
			var maxWidth = 1200;
			return window.innerWidth < maxWidth;
		}
	});
</script>
<script>
	$(function (){
		$('#form1').on('submit', function (e) {
			let index = layer.load(1);
			e.preventDefault();
			let email = $("#InputEmail1").val();
			let password = $("#InputPassword1").val();
			if(email&&password){
				$.ajax({
					timeout:5000,
					type:'POST',
					url:window.Http+"/login",
					data:JSON.stringify({"username":email,"password":password}),
					contentType: 'application/json;charset=utf-8',
					success:function (res){
						if(res.code === 200){
							sessionStorage.setItem('Token', res.token);
							if(res.token){
								$.ajax({
									timeout:30000,
									type:'GET',
									url:window.Http+"/getInfo",
									headers: {
										'Authorization':"Bearer "+sessionStorage.getItem('Token')
									},
									success:function (userRes){
										if(res.code === 200){
											sessionStorage.setItem('userId',userRes.user.userId);
											sessionStorage.setItem('UserInfo',JSON.stringify(userRes.user));
											if ("WebSocket" in window) {
												window.ws = new WebSocket(window.monitorWebsocket);
												let timeout = setTimeout(function() {
													layer.msg('监管程序连接失败');
													layer.open({
														content: '连接监管程序失败,是否安装数据监管程序？',
														btn: ['安装', '不安装'],
														yes: function (index, layero) {
															let index1 = layer.load(1);
															monitorDownload()
																	.then((data)=>{
																		layer.close(index1);
																		window.location.href=data.Data;
																	})
																	.catch(()=>{
																		layer.close(index1);
																		layer.msg('请求下载失败')
																	})
															// .finally(()=>{
															//   layer.msg('请求下载失败')
															//   window.location.href = './index.html';
															// })
														},
														btn2: function (index,layero){
															layer.close(index);
															window.location.href = './index.html';
														}
													})
													window.ws.close();
												}, 3000); // 3 seconds

												//     //连接成功时触发
												ws.onopen = function () {
													console.log('连接成功')
													// 发送登录信息
													ws.send(JSON.stringify(loginInfo))
													// 接收消息
													ws.onmessage = function (MessageEvent) {
														console.log('MessageEvent.data', MessageEvent.data)
														if (MessageEvent.data) {
															const resData = JSON.parse(MessageEvent.data);
															console.log('resData', resData)
															if (resData.Cmd === 'LoginRet' && resData.Code === 200) {
																layer.msg('登录监管程序成功')
																setTimeout(function () {
																	window.location.href = './index.html';
																}, 2000)
															} else {
																layer.msg('登录监管程序失败')
															}
														} else {
															console.log('没有data')
														}
													}
												}
												//     //连接失败时触发
												ws.onerror = function (){
													layer.msg('监管程序连接失败');
													layer.open({
														content: '连接监管程序失败,是否安装数据监管程序？',
														btn: ['安装', '不安装'],
														yes: function (index, layero) {
															let index1 = layer.load(1);
															monitorDownload()
																	.then((data)=>{
																		layer.close(index1);
																		window.location.href=data.Data;
																	})
																	.catch(()=>{
																		layer.close(index1);
																		layer.msg('请求下载失败')
																	})
															// .finally(()=>{
															//   layer.msg('请求下载失败')
															//   window.location.href = './index.html';
															// })
														},
														btn2: function (index,layero){
															layer.close(index);
															window.location.href = './index.html';
														}
													})
												}
												// 登录信息
												const loginInfo={
													Cmd:"Login",
													user: {
														name: email,
														password:password
													}
												}
												console.log(JSON.stringify(loginInfo) )
												// ws.send(JSON.stringify(loginInfo))
												// 连接成功
												if(ws.readyState===WebSocket.OPEN){
													console.log('连接成功')
													// 发送登录信息
													ws.send(JSON.stringify(loginInfo))
													// 接收消息
													ws.onmessage=function (MessageEvent) {
														console.log('MessageEvent.data', MessageEvent.data)
														if (MessageEvent.data){
															const resData=JSON.parse(MessageEvent.data);
															console.log('resData', resData)
															if(resData.Cmd==='LoginRet' &&resData.Code===200){
																layer.msg('登录监管程序成功')
																setTimeout(function () {
																	window.location.href = './index.html';
																},2000)
															}else {
																layer.msg('登录监管程序失败')
															}
														}else {
															console.log('没有data')
														}
													}
												}else {
													console.log('window.ws.readyState', ws.readyState)
													console.log('WebSocket.OPEN',WebSocket.OPEN)
												}

											}else {
												layer.msg('您的浏览器不支持 WebSocket!')
											}
											// monitorStatus()
											// 	  .then(()=>{
											// 		  monitorLogin(email,password)
											// 				  .then(()=>{
											// 					  layer.close(index);
											// 					  layer.msg('登录监管程序成功')
											// 					  setTimeout(function () {
											// 						  window.location.href = './index.html';
											// 					  },1000)
											// 				  })
											// 				  .catch(()=>{
											// 					  layer.close(index);
											// 					  layer.msg('登录监管程序失败')
											// 				  })
											// 				  // .finally(()=>{
											// 					//   layer.msg('登录监管程序失败')
											// 				  // })
											// 	  })
											// 	  .catch(()=>{
											// 		  layer.close(index);
											// 		  layer.open({
											// 			  content: '连接监管程序失败,是否安装数据监管程序？',
											// 			  btn: ['安装', '不安装'],
											// 			  yes: function (index, layero) {
											// 				  let index1 = layer.load(1);
											// 				  monitorDownload()
											// 						  .then((data)=>{
											// 							  layer.close(index1);
											// 							  window.location.href=data.Data;
											// 						  })
											// 						  .catch(()=>{
											// 							  layer.close(index1);
											// 							  layer.msg('请求下载失败')
											// 						  })
											// 						  // .finally(()=>{
											// 							//   layer.msg('请求下载失败')
											// 							//   window.location.href = './index.html';
											// 						  // })
											// 			  },
											// 			  btn2: function (index,layero){
											// 					   layer.close(index);
											// 					   window.location.href = './index.html';
											// 			   }
											// 		  })
											// 	  })
											// $.ajax({
											//    url:window.monitorHttp + 'api/v1/status',
											//    type: "POST",
											//    headers: {
											// 	   'Authorization': "Bearer " + sessionStorage.getItem('Token')
											//    },
											//    timeout:3000,
											//    data:JSON.stringify({
											// 	   "Cmd":"Status",
											// 	   "Program":"Front",
											//    }),
											//    success: function (res) {
											// 	   console.log('res', res)
											// 	   let data;
											// 	   try {
											// 		   data=JSON.parse(res)
											// 		   // console.log('data', data)
											// 	   }catch (e) {
											// 		   layer.msg(e)
											// 	   }
											// 	   if (data.Code === 200) {
											// 		   monitorLogin(email,password).then(()=>{
											// 			   layer.msg('登录监管程序成功')
											// 		   window.location.href = './index.html';
											// 		   })
											// 				   .catch(()=>{
											// 					   layer.msg('登录监管程序失败')
											// 				   })
											// 	   } else {
											// 		   layer.open({
											// 			   content:'连接监管程序失败,是否安装数据监管程序？',
											// 			   btn: ['安装', '不安装'],
											// 			   yes:function (index,layero) {
											// 				   // window.location.href='download'
											// 				   $.ajax({
											// 					   url: window.monitorHttp + 'api/v1/monitor/download',
											// 					   type: 'GET',
											// 					   headers: {
											// 						   'Authorization': "Bearer " + sessionStorage.getItem('Token')
											// 					   },
											// 					   success: function (res) {
											// 						   let data;
											// 						   try {
											// 							   data=JSON.parse(res)
											// 						   }catch (e) {
											// 							   layer.msg(e)
											// 						   }
											// 						   if (data.Code === 200) {
											// 							   window.location.href=data.Data;
											// 						   } else {
											// 							   layer.msg(data.Msg||'请求下载失败')
											// 						   }
											// 					   },
											// 					   error:function () {
											// 						   layer.msg('请求下载失败')
											// 						   window.location.href = './index.html';
											// 					   }
											// 				   })
											// 				 // setTimeout(function () {
											// 					//  layer.close(index);
											// 					//  window.location.href = './index.html';
											// 				 // },1000)
											// 			   },
											// 			   btn2: function (index,layero){
											// 				   layer.close(index);
											// 				   window.location.href = './index.html';
											// 			   }
											// 		   })
											// 		   // layer.msg('连接监管程序失败')
											// 	   }
											//    },
											//    error:function (e){
											// 	   layer.open({
											// 		   content:'连接监管程序失败,是否安装数据监管程序？',
											// 		   btn: ['安装', '不安装'],
											// 		   yes:function (index,layero) {
											// 			   //TODO　数据监管程序下载URL
											// 			   // window.location.href=''
											// 			   $.ajax({
											// 				   url: window.monitorHttp + 'api/v1/monitor/download',
											// 				   type: 'GET',
											// 				   headers: {
											// 					   'Authorization': "Bearer " + sessionStorage.getItem('Token')
											// 				   },
											// 				   success: function (res) {
											// 					   let data;
											// 					   try {
											// 						   data=JSON.parse(res)
											// 					   }catch (e) {
											// 						   layer.msg(e)
											// 					   }
											// 					   if (data.Code === 200) {
											// 						   window.location.href=data.Data;
											// 					   } else {
											// 						   layer.msg(data.Msg||'请求下载失败')
											// 					   }
											// 				   },
											// 				   error:function () {
											// 					   layer.msg('请求下载失败')
											// 					   window.location.href = './index.html';
											// 				   }
											// 			   })
											// 			   // setTimeout(function () {
											// 				//    layer.close(index);
											// 				//    window.location.href = './index.html';
											// 			   // },1000)
											// 		   },
											// 		   btn2: function (index,layero){
											// 			   layer.close(index);
											// 			   window.location.href = './index.html';
											// 		   }
											// 	   })
											//    }
											// })
										} else if (res.code == 401){
											layer.close(index);
											window.location.href = '../login.html';
										}
									},error:function (err){
										layer.close(index);
										layer.msg("服务器异常，请稍后重试");
									},
								})
							}



						}else{
							layer.msg(res.msg||"无法登陆，账号或密码错误");
						}
					},
					error:function(err){
						layer.msg("服务器异常");
					}
				})
			}
		})
		// 记住我
		$('#InputEmail1').val(localStorage.getItem('username1'))
		$('#InputPassword1').val(localStorage.getItem('password1'))
		$('#exampleCheck1').click(function(){
			if($('#exampleCheck1').is(':checked')){
				var username1=$('#InputEmail1').val()
				var password1=$('#InputPassword1').val()
				localStorage.setItem('username1',username1)
				localStorage.setItem('password1',password1)
			}else {
				localStorage.removeItem('username1');
				localStorage.removeItem('password1');
			}
		})
		function monitorStatus() {
			return new Promise(function (resolve, reject) {
				$.ajax({
					url: window.monitorHttp + 'api/v1/status',
					type: "POST",
					headers: {
						'Authorization': "Bearer " + sessionStorage.getItem('Token')
					},
					data:JSON.stringify({
						"Cmd":"Status",
						"Program":"Front",
					}),
					success: function (res) {
						const data=isJSON(res);
						if (data.Code === 200) {
							resolve(data)
						} else {
							layer.msg(data.Msg)
							layer.msg('连接监管程序失败')
							reject(new Error('连接监管程序失败'))
						}
					},
					error:function () {
						reject(new Error('连接监管程序失败'))
					}
				})
			})
		}

		/**
		 * @description:函数描述 登录监管程序
		 * @return Promise
		 */
		function monitorLogin(name,password) {
			return new Promise(function (resolve, reject) {
				$.ajax({
					url: window.monitorHttp +'api/v1/user/login',
					type: 'POST',
					headers: {
						'Authorization': "Bearer " + sessionStorage.getItem('Token'),
						'content-Type': "application/json;charset=utf-8"
					},
					data:JSON.stringify({
						Cmd:"Login",
						user: {
							name: name,
							password:password
						}
					}),
					success: function (res) {
						const data=isJSON(res);
						if (data.Code === 200) {
							resolve(data)
						} else {
							layer.msg(data.Msg)
							reject(new Error('登录监管程序失败'))
						}
					},
					error:function () {
						reject(new Error('登录监管程序失败'))
					}
				})
			})
		}
		/**
		 * @description:函数描述 判断是否是JSON 并返回js对象
		 * @return Object
		 * @param obj
		 */

		function isJSON(obj) {
			try {
				return JSON.parse(obj);
			} catch (e) {
				return obj;
			}
		}
		/**
		 * @description:函数描述 下载监管程序
		 * @return Promise
		 */

		function monitorDownload() {
			return new Promise(function (resolve, reject) {
				$.ajax({
					url: window.monitorDownloadHttp + 'api/v1/monitor/download',
					type: 'GET',
					// headers: {
					// 	'Authorization': "Bearer " + sessionStorage.getItem('Token')
					// },
					success: function (res) {
						const data=isJSON(res);
						if (data.Code === 200) {
							resolve(data)
						} else {
							reject(new Error('监管程序下载失败'))
						}
					},
					error:function () {
						reject(new Error('监管程序下载失败'))
					}
				})
			})
		}

	})

</script>
</body>
</html>
